﻿using System;
using System.Net;
using System.Net.Mail;

namespace YPF.Common.邮件
{
    public  static class EmailHelper
    {
        /// <summary>
        /// 使用QQ邮箱发送邮件(qq邮箱必须开启smtp服务)
        /// </summary>
        /// <param name="fromEmail">发件人地址</param>
        /// <param name="fromEailToken">由于腾讯的邮箱，在使用第三方发送邮件的时候，需要使用授权码进行登录</param>
        /// <param name="toEail">收件人地址</param>
        /// <param name="subjectStr">标题</param>
        /// <param name="bodyStr">内容</param>
        /// <returns></returns>
        public static bool SendMessageForQQ(string fromEmail,string fromEailToken,string toEail,string subjectStr,string bodyStr)
      {
            try
            {
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.qq.com";
                MailMessage mm = new MailMessage();
                client.Port = 25;
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                mm.From = new MailAddress(fromEmail);
                mm.To.Add(new MailAddress(toEail));
                mm.Subject = subjectStr;
                mm.Body = bodyStr;
                mm.IsBodyHtml = false;
                mm.Priority = MailPriority.High;
                //由于腾讯的邮箱，在使用第三方发送邮件的时候，需要使用授权码进行登录。
                client.Credentials = new NetworkCredential(fromEmail, fromEailToken);
                client.Send(mm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
          return true;
      }
    }
}
